package com.sch.wanandroid.ui.main.discover.system

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sch.wanandroid.entity.NavigationBean
import com.sch.wanandroid.entity.SystemBean
import com.sch.wanandroid.network.NetWorkManager
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * Created by Sch.
 * Date: 2020/12/14
 * description:
 */
class SystemViewModel : ViewModel() {
    private val _treeList = MutableLiveData<List<SystemBean>>()
    val treeListLiveData: LiveData<List<SystemBean>> = _treeList

    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> = _errorMsg
    fun getSystemData() {
        viewModelScope.launch {
            try {
                val result = NetWorkManager.apiService.getTree()
                if (result.errorCode != 0) {
                    _errorMsg.value = result.errorMsg
                } else {
                    _treeList.value = result.data
                }
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }
}